//
//  TabBar.m
//  框架
//
//  Created by 刘明 on 16/3/21.
//  Copyright © 2016年 LM. All rights reserved.
//

#import "TabBar.h"

@interface TabBar()
@property(nonatomic,weak)UIButton *plusButton;
@end

@implementation TabBar
-(instancetype)initWithFrame:(CGRect)frame
{

    if (self = [super initWithFrame:frame]) {
        [self setupPlusButton];
        
    }
    return self;
    
}


/**
 *  重写layoutSubviews自定义布局
 */
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //设置所有控件的frame
    [self setupAllTabBarButtonFrame];
    //添加加号按钮的Frame
    [self setupPlusButtonFrame];
}

//设置所有控件的frame
-(void)setupAllTabBarButtonFrame
{
    if (/* DISABLES CODE */ (1)==0) {
        NSUInteger count = self.subviews.count;
        
        Class class = NSClassFromString(@"UITabBarButton");
        for (int i = 0; i < count; i++) {
            UIView * view = self.subviews[i];
            
            if ([view isKindOfClass:class]) {
                view.width = WIDTH/4;
            }
        }

        
    }
      //遍历所有的子控件
    int index = 0;
    
    for (UIView *tabarButton in self.subviews)
    {
        
        //如果不是UITabBarButton,直接跳过
        if (![tabarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        
        //根据索引调整位置
        
        //设置某一个tabbarButton
        [self setTabBarButtonFrame:tabarButton atIndex:index];
        
        
        //遍历UITabBarButton中所有子控件
        //数组中类型是id
        //设置按钮的文字颜色
//        [self setTabBarButtonTextColor:tabarButton atIndex:index];
        
        //索引增加
        index++;
    }


}

//设置某一个tabbarButtonFrame
-(void)setTabBarButtonFrame:(UIView *)tabarButton  atIndex:(int)index
{
    //计算Button尺寸
    CGFloat buttonW = self.width / (self.items.count + 1);
    CGFloat butttonH = self.height;
    
    tabarButton.width = buttonW;
    tabarButton.height = butttonH;
    
    if (index >= 2) {
        tabarButton.x = buttonW *(index + 1);
    }
    else
    {
        tabarButton.x = buttonW * index;
    }
    
    tabarButton.y = 0;
    
    
}

//设置tabbarButton背景颜色
-(void)setTabBarButtonTextColor:(UIView *)tabarButton  atIndex:(int)index
{
    
    //获取选中索引的下标
    long selectedIndex = [self.items indexOfObject:self.selectedItem];
    
    for (UILabel *label in tabarButton.subviews)
        
    {
        //说明不是一个Label
        if (![label isKindOfClass:[UILabel class]]) continue;
        
        
        if ([label isKindOfClass:[UILabel class]]) {
            label.font = [UIFont systemFontOfSize:10];
            //设置Button选中设置颜色
            if (selectedIndex == index) {
                label.textColor = [UIColor orangeColor];
            }
            else
            {
                
                label.textColor = [UIColor blackColor];
            }
            
        }
        
    }
    
    
    
}


//添加加号按钮
-(void)setupPlusButton
{
    
    UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置背景
    [plusButton setBackgroundImage:[UIImage imageWithName:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
    //设置图片
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    
    [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:plusButton];
    
    self.plusButton = plusButton;

}


//中间按钮事件
-(void)plusClick
{
 //通知代理
    if ([self.Delegate respondsToSelector:@selector(taBarDidClickPlusButton:)]) {
        [self.Delegate taBarDidClickPlusButton:self];
    }

}

//设置加号按钮的Frame
-(void)setupPlusButtonFrame
{
    self.plusButton.size = self.plusButton.currentBackgroundImage.size;
    self.plusButton.centerX = self.width/2;
    self.plusButton.centerY = self.height/2;
    
}

@end





























